import 'package:dio/dio.dart';

import '../http/dio_instance.dart';
import 'datas/app_check_update_model.dart';
import 'datas/common_website_data.dart';
import 'datas/home_banner_model.dart';
import 'datas/home_list_model.dart';
import 'datas/knowledge_detail_list_model.dart';
import 'datas/knowledge_list_model.dart';
import 'datas/my_collects_model.dart';
import 'datas/search_hot_keys_data.dart';
import 'datas/search_list_model.dart';
import 'datas/user_info_model.dart';

class Api{
  static Api? _instance;

  Api._();

  static Api instance() {
    return _instance ??= Api._();
  }
  //轮播图
  Future<List<HomeBannerModel?>?> getBanner() async{
    Response res = await DioInstance.instance().get(path: "banner/json");
    HomeBannerListModel bannerData =  HomeBannerListModel.fromJson(res.data);
    return bannerData.bannerList;
  }
  //首页列表
  Future<List<HomeListItemData>?> getHomeList(String pageIndex) async{
    Response res = await DioInstance.instance().get(path: "article/list/$pageIndex/json");
    HomeListModel homeData =  HomeListModel.fromJson(res.data);
    return homeData.datas;
  }
  //首页置顶列表
  Future<List<HomeListItemData>?> getHotList() async{
    Response res = await DioInstance.instance().get(path: "article/top/json");
    HomeTopListModel homeData =  HomeTopListModel.fromJson(res.data);
    return homeData.dataList;
  }

  //常用网站
  Future<List<CommonWebsiteData>?> getWebsiteList() async{
    Response res = await DioInstance.instance().get(path: "friend/json");
    CommonWebsiteListData websiteList =  CommonWebsiteListData.fromJson(res.data);
    return websiteList.websiteListData;
  }

  //热点keys
  Future<List<SearchHotKeysData>?> getSearchHotKeysList() async{
    Response res = await DioInstance.instance().get(path: "hotkey/json");
    SearchHotKeysListData searchHotKeysList =  SearchHotKeysListData.fromJson(res.data);
    return searchHotKeysList.searchHotKeysListData;
  }

  //登录
  Future<UserInfoModel> login({String? name,String? password}) async{
    Response res = await DioInstance.instance().post(path: "user/login",queryParameters: {
    "username":name,
    "password":password,
    });
    return UserInfoModel.fromJson(res.data);
  }
  //推出登录
  Future<bool?> loginOut() async{
    Response res = await DioInstance.instance().get(path: "user/logout/json");
    return boolCallBack(res.data);
  }
  //注册
  Future<UserInfoModel> register({String? name,String? password,String?rePassword}) async{
    Response res = await DioInstance.instance().post(path: "user/register",queryParameters: {
      "username":name,
      "password":password,
      "repassword":rePassword
    });
    return UserInfoModel.fromJson(res.data);
  }
  //获取体系数据
  Future<List<KnowledgeModel?>> knowledgeList() async{
    Response res = await DioInstance.instance().get(path: "tree/json");
    return KnowledgeListModel.fromJson(res.data).list;
  }
  //获取体系明细数据
  Future <List<KnowledgeDetailItem>?> detailKnowledgeList(String?pageCount,String? cid)async{
    Response response= await DioInstance.instance().get(path: "article/list/$pageCount/json", params: {"cid": cid});
    return KnowledgeDetailListModel.fromJson(response.data).datas;
  }
  //收藏
  Future<bool?> collect(String? id) async{
    Response res = await DioInstance.instance().post(path: "lg/collect/$id/json");
    return boolCallBack(res.data);
  }
  //取消收藏
  Future<bool?> unCollect(String? id) async{
    Response res = await DioInstance.instance().post(path: "lg/uncollect_originId/$id/json");
    return boolCallBack(res.data);
  }
  //搜索数据
  Future <List<SearchListItemModel>?> search({String? keyWord}) async{
    Response res = await DioInstance.instance().post(path: "article/query/0/json",queryParameters: {"k":keyWord});
    return SearchListModel.fromJson(res.data).datas;
  }

  //获取收藏列表
  Future<List<MyCollectItemModel>?> getMyCollects(String pageCount) async {
    Response rsp = await DioInstance.instance().get(path: "lg/collect/list/$pageCount/json");
    MyCollectsModel? model = MyCollectsModel.fromJson(rsp.data);
    if (model.datas != null && model.datas?.isNotEmpty == true) {
      return model.datas;
    }
    return [];
  }
  //检查更新 checkUpdate
  ///检查app新版本
  Future<AppCheckUpdateModel?> checkUpdate() async {
    DioInstance.instance().changeBaseUrl("https://www.pgyer.com/");
    Response response = await DioInstance.instance().post(
        path: "apiv2/app/check",
        // queryParameters: {
        //   "_api_key": "57c543d258a34f8565748561de50b6e6",
        //   "appKey": "2639f784ce9ee850532074b7b0534e62"
        // });
        queryParameters: {
          "_api_key": "91ae503dac692858f16f50a2e3a7d48a",
          "appKey": "d9ee28047b5bac148834e99582a31712"
        });

    DioInstance.instance().changeBaseUrl("https://www.wanandroid.com/");

    return AppCheckUpdateModel.fromJson(response.data);
  }
  bool? boolCallBack(dynamic data){
    if(data != null && data is bool){
      return data;
    }
    return false;
  }
}